// ignore_for_file: deprecated_member_use, prefer_const_literals_to_create_immutables, prefer_const_constructors



import 'package:flutter/material.dart';
import 'package:flutterdemo/common/calc.dart';
import 'chat.dart';
import 'home.dart';
import 'me.dart';
import 'shop.dart';
import 'classification.dart';
import '../../components/drawer_head.dart';

class Tabs extends StatefulWidget {
  // ignore: prefer_const_constructors_in_immutables
  Tabs({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> with SingleTickerProviderStateMixin {
  int _curIndex = 0;
  // ignore: prefer_typing_uninitialized_variables
  var _pageController;
  // ignore: non_constant_identifier_names
  List<Widget> TabsPage = [
    HomePage(),
    ClassifiPage(),
    Chat(),
    ShopPage(),
    MePage()
  ];
  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _curIndex);
  }

  AppBar _appbar() {
    return AppBar(
      leading: Icon(
        Icons.settings,
        color: Colors.black54,
      ),
      backgroundColor: Colors.white,
      toolbarHeight: _curIndex == 4 ? 0 : 50,
      centerTitle: true,
      elevation: 0,
      title: _curIndex == 4 ? Text("我的") : searchWidget(),
    );
  }

  @override
  Widget build(BuildContext context) {
    Calc.init(context);
    return Scaffold(
      drawer: DrawerHead(),
      appBar: _appbar(),
      backgroundColor: Color.fromRGBO(233, 233, 233, 1),
      body: PageView(
        controller: _pageController,
        onPageChanged: (index) {
          setState(() {
            _curIndex = index;
          });
        },
        children: TabsPage,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _curIndex,
        onTap: (index) {
          setState(() {
            _curIndex = index;
            _pageController.jumpToPage(_curIndex);
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text("首页"),
          ),
          BottomNavigationBarItem(icon: Icon(Icons.shop), title: Text("分类")),
          BottomNavigationBarItem(
              icon: Icon(Icons.language), title: Text("消息")),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_cart), title: Text("购物车")),
          BottomNavigationBarItem(icon: Icon(Icons.group), title: Text("我的")),
        ],
      ),
    );
  }

  // 顶部搜索栏
  searchWidget<Widget>() {
    return Ink(
      child: InkWell(
        onTap: (){
          Navigator.pushNamed(context, "search");
        },
        child: Center(
          child: TextField(
              enabled: false,
              decoration: InputDecoration(
                hintText: "搜一搜",
                isCollapsed: true,
                prefixIcon: Icon(Icons.search),
                contentPadding: EdgeInsets.all(5),
                border: InputBorder.none,
              )),
        ),
      ),
      height: 30,
      // padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
      decoration: BoxDecoration(
          color: Color.fromRGBO(233, 233, 233, 1),
          borderRadius: BorderRadius.all(Radius.circular(25.0))),
    );
  }
}
